//
//  HBReSetTradpasswordCtrl.m
//  HuoBao
//
//  Created by 于会海 on 15/12/3.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBReSetTradpasswordCtrl.h"
#import "CheckValid.h"

@interface HBReSetTradpasswordCtrl ()<UITextFieldDelegate>
{
    NSArray* fieldArr;
}
- (IBAction)getVerCode:(UIButton *)sender;
- (IBAction)nextBtnClicked:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *alertLabel;
@property (weak, nonatomic) IBOutlet UITextField *phoneNum;
@property (weak, nonatomic) IBOutlet UITextField *verCode;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@end

@implementation HBReSetTradpasswordCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)buildLayout{
    self.title = @"重置交易密码";
    self.phoneNum.delegate = self;
    self.verCode.delegate = self;
    self.getCodeBtn.layer.cornerRadius = 4.0;
    self.nextBtn.layer.cornerRadius = 4.0;
    fieldArr = @[self.phoneNum,self.verCode];
    for (UITextField* textField in fieldArr) {
        [self setTextFieldPlaceHolder:textField];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setTextFieldPlaceHolder:(UITextField*)textField  {
    [textField setValue:[LCCommon getColor:@"5f606b"] forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
}

- (IBAction)getVerCode:(UIButton *)sender {
    LLog(@"请求验证码");
    self.verCode.text = @"";
    [self checkFieldTextAndVerCodeCheck:NO];
    if (![CheckValid isValidatePhone:self.phoneNum.text]) {
        [self.alertLabel setHidden:NO];
        self.alertLabel.text = @"手机号输入不正确";
        self.phoneNum.text = @"";
        self.verCode.text = @"";
        [self.phoneNum becomeFirstResponder];
        return;
    }
    [self setTimerDefault];
    [self makeTime];
}

- (IBAction)nextBtnClicked:(UIButton *)sender {
    LLog(@"下一步");
}

- (BOOL)checkFieldTextAndVerCodeCheck:(BOOL)check{
    for (UITextField* textField in fieldArr) {
        BOOL isEmpty = [CheckValid checkStrIsEmpty:textField.text];
        if (isEmpty == YES) {
            [self.alertLabel setHidden:NO];
            [textField becomeFirstResponder];
            NSString* alertString;
            if (textField == self.phoneNum) {
                alertString = @"请输入手机号码";
            }
            if (textField == self.verCode) {
                if (check == YES) {
                    if (textField == self.verCode) {
                        alertString = @"请输入验证码";
                    }
                }else{
                    [self.alertLabel setHidden:YES];
                    [self.alertLabel setText:@""];
                }
            }
            self.alertLabel.text = alertString;
            return NO;
        }else{
            if (textField == self.phoneNum) {
                if (![CheckValid isValidatePhone:self.phoneNum.text]) {
                    [self.alertLabel setHidden:NO];
                    self.alertLabel.text = @"手机号输入不正确";
                    self.phoneNum.text = @"";
                    self.verCode.text = @"";
                    [self.phoneNum becomeFirstResponder];
                    return NO;
                }
            }
        }
    }
    return YES;
}


- (void)setTimerDefault{
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f",[NSDate date].timeIntervalSince1970 + 60] forKey:ADDBANKCARD_CODE_TIME];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)makeTime{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSTimeInterval timeLast = [[[NSUserDefaults standardUserDefaults] objectForKey:ADDBANKCARD_CODE_TIME] longLongValue];
        if (timeLast <= 0){
            self.getCodeBtn.enabled = YES;
            [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateDisabled];
            [self.getCodeBtn setTitleColor:[LCCommon getColor:@"ffffff"] forState:UIControlStateNormal];
            self.getCodeBtn.backgroundColor = [LCCommon getColor:@"F24545"];
            return;
        }
        NSTimeInterval now = [NSDate date].timeIntervalSince1970;
        if (now >= timeLast){
            self.getCodeBtn.enabled = YES;
            [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateDisabled];
            [self.getCodeBtn setTitleColor:[LCCommon getColor:@"ffffff"] forState:UIControlStateNormal];
            self.getCodeBtn.backgroundColor = [LCCommon getColor:@"F24545"];
            return;
        }
        self.getCodeBtn.enabled = NO;
        [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%d秒后重新获取",(int)(timeLast- now)] forState:UIControlStateNormal];
        [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%d秒后重新获取",(int)(timeLast- now)] forState:UIControlStateDisabled];
        self.getCodeBtn.backgroundColor = [LCCommon getColor:@"eeeeee"];
        [self.getCodeBtn setTitleColor:[LCCommon getColor:@"666666"] forState:UIControlStateNormal] ;
        [self performSelector:@selector(makeTime) withObject:nil afterDelay:0.01];
    });
}

@end
